package com.ellricky.baselib.ui.clue;

import android.content.Intent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.andr.common.ui.activity.BaseToolBarActivity;
import com.andr.common.ui.mvp.BasePresenter;
import com.andr.common.util.EmptyUtil;
import com.andr.common.util.ToastUtil;
import com.ellricky.baselib.BaseConstant;
import com.ellricky.baselib.R;
import com.ellricky.baselib.cache.LocalCacheUtil;
import com.ellricky.baselib.dialog.picker.EPickerStrListener;
import com.ellricky.baselib.dialog.picker.EPickerType;
import com.ellricky.baselib.dialog.picker.EPickerUtil;
import com.ellricky.baselib.http.HttpUtils;
import com.ellricky.baselib.ui.SelectActiveActivity;
import com.ellricky.baselib.ui.SelectIntroduceActivity;
import com.ellricky.baselib.util.EAppUtil;
import com.ellricky.baselib.util.NewECallback;
import com.ellricky.baselib.widget.EditItemView;
import com.flyco.roundview.RoundTextView;

import java.util.HashMap;
import java.util.Map;


public class AddClueActivity extends BaseToolBarActivity<Void, BasePresenter<Void>> implements EPickerStrListener {

    private EditItemView etName;
    private EditItemView etContact;
    private EditItemView etAge;
    private EditItemView etRemark;
    private EditItemView etSchool;
    private TextView tvSelect;

    private RadioGroup radioGroup;
    private RadioButton rbMan, rbWoman;

    private RoundTextView btnSubmit;

    private EPickerUtil ePickerUtil;
    private int position;
    private String clueFrom = ""; //来源类型(35活动，36进店，37转介绍，38拉访，39市场，60地推等)
    private String clueFromId; //来源id 转介绍人id或活动id
    private String clueName;
    private String activeName;

    @Override
    public int getContentLayout() {
        return R.layout.base_activity_add_clue;
    }

    @Override
    protected void initUIView() {
        super.initUIView();
        ePickerUtil = new EPickerUtil(this);
        mTvTitle.setText(R.string.base_add_clue);

        etName = findViewById(R.id.etName);
        etContact = findViewById(R.id.etContact);
        etAge = findViewById(R.id.etAge);
        etSchool = findViewById(R.id.etSchool);
        etRemark = findViewById(R.id.etRemark);
        radioGroup = findViewById(R.id.rgClueFrom);
        tvSelect = findViewById(R.id.tvSelect);

        rbMan = findViewById(R.id.rbMan);
        rbWoman = findViewById(R.id.rbWoman);
        btnSubmit = findViewById(R.id.btnSubmit);

        etContact.setInputPhone();
        etAge.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
        tvSelect.setOnClickListener(this);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbA) {
                    clueFrom = "36";
                    clueFromId = "";
                    tvSelect.setText("");
                    position = 0;
                } else if (checkedId == R.id.rbB) {
                    position = 1;
                    tvSelect.setText("请选择转介绍人");
                } else if (checkedId == R.id.rbC) {
                    position = 2;
                    tvSelect.setText("请选择活动");
                }
            }
        });
    }

    @Override
    protected void onClickView(View v) {
        super.onClickView(v);
        if (v == btnSubmit) postSubmit();
        if (v == etAge) {
            ePickerUtil.showMarketAgePicker(0, this);
            return;
        }

        if (v == tvSelect) {
            switch (position) {
                case 1:
                    EAppUtil.jumpForResult(this, SelectIntroduceActivity.class, 10000);
                    break;
                case 2:
                    EAppUtil.jumpForResult(this, SelectActiveActivity.class, 10001);
                    break;
            }
        }
    }

    @Override
    public void onSelectItem(String content, int picPos, int pickType) {
        if (pickType == EPickerType.PICK_AGE) etAge.setValue(content);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            switch (requestCode) {
                case 10000:
                    clueFrom = "37";
                    clueFromId = data.getStringExtra(BaseConstant.KEY_ID);
                    clueName = data.getStringExtra(BaseConstant.KEY_NAME);
                    tvSelect.setText(clueName);
                    break;
                case 10001:
                    clueFrom = "35";
                    clueFromId = data.getStringExtra(BaseConstant.KEY_ID);
                    activeName = data.getStringExtra(BaseConstant.KEY_NAME);
                    tvSelect.setText(activeName);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void postSubmit() {
        String name = etName.getValue();
        if (EmptyUtil.isEmpty(name)) {
            ToastUtil.show(this, R.string.base_input_name);
            return;
        }

        String tel = etContact.getValue();
        if (EmptyUtil.isEmpty(tel)) {
            ToastUtil.show(this, R.string.base_input_contact);
            return;
        }

        String age = etAge.getValue();
        if (EmptyUtil.isEmpty(age)) {
            ToastUtil.show(this, R.string.base_select_birthday_date);
            return;
        }

        String remark = etRemark.getValue();
        if (EmptyUtil.isEmpty(remark)) {
            ToastUtil.show(this, R.string.base_remark_input);
            return;
        }

        String sex = "";
        if (rbMan.isChecked()) sex = "男";
        if (rbWoman.isChecked()) sex = "女";
        if (EmptyUtil.isEmpty(sex)) {
            ToastUtil.show(this, R.string.base_select_sex);
            return;
        }

        String fromSchool = etSchool.getValue();

        String url = EAppUtil.getApiHost();
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("action", getAction());
        paramMap.put("school_id", LocalCacheUtil.getSchoolId());
        paramMap.put("clue_name", name);
        paramMap.put("clue_tel", tel);
        paramMap.put("clue_age", age);
        paramMap.put("clue_sex", sex);
        paramMap.put("clue_study_school", fromSchool);
        paramMap.put("clue_from_dic_id", clueFrom);
        paramMap.put("clue_from_target_id", clueFromId);
        paramMap.put("remark", remark);
        paramMap.put("eid", LocalCacheUtil.getUid());

        HttpUtils.getInstance().postFrom(url, paramMap, new NewECallback<Object>(this) {
            @Override
            public void onSuccess(Object data, String msg) {
                ToastUtil.show(AddClueActivity.this, "提交成功");
                setResult(BaseConstant.RESULT_CODE_ADD);
                finish();
            }
        });
    }

    private String getAction() {
        switch (LocalCacheUtil.getCardType()) {
            case BaseConstant.CARD_CONSULTANT:
                return "saleAddClue";
            case BaseConstant.CARD_MARKET:
                return "maketAddClue";
            default:
                return "addClue";
        }
    }
}
